package net.goodapp.util;

import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Plain Old Java Object(POJO)对象的属性名及类名的格式化辅助类
 * @author zhou.jin-qiao
 *
 */
public class NamingConventions {
	private NamingConventions(){}
	/** 
     * 对象属性转换为字段  例如：userName to user_name 
     * @param property 字段名 
     * @return 
     */  
    public static String propertyToField(String property) {  
        if (null == property) {  
            return "";  
        }
        
        char[] chars = property.toCharArray();  
        StringBuilder sb = new StringBuilder();   
        for (char c : chars) {  
            if (CharUtils.isAsciiAlphaUpper(c)) {  
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));  
            } else {  
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    }  
  
    /** 
     * 字段转换成对象属性 例如：user_name -> userName及ID -> id或USERNAME -> username
     * @param field 
     * @return 
     */  
    public static String fieldToProperty(String field) {  
        if (null == field) {  
            return "";  
        }
        //如果数据库字段中没有'_'字符，则将整个字符串全部转换成小写
        if(field.indexOf('_')<0){
        	return field.toLowerCase();
        }
        
        char[] chars = field.toCharArray();
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; i < chars.length; i++) {  
            char c = chars[i];  
            if (c == '_') {  
                int j = i + 1;  
                if (j < chars.length) {  
                    sb.append(StringUtils.upperCase(CharUtils.toString(chars[j])));  
                    i++;  
                }  
            } else {
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    } 
    
    /** 
     * 对象属性转换为字段  例如：userName to user_name 
     * @param className 类名
     * @return 
     */  
    public static String classToField(String className) {  
        if (null == className) {  
            return "";  
        }  
        char[] chars = className.toCharArray();  
        StringBuilder sb = new StringBuilder(); 
        for (char c : chars) {  
            if (CharUtils.isAsciiAlphaUpper(c)) {  
                sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));  
            } else {  
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    }  
  
    /** 
     * 表名转换成类名 例如：user_name to UserName 
     * @param field 
     * @return 
     */  
    public static String tableNameToClassName(String field) {  
        if (null == field) {  
            return "";  
        }  
        char[] chars = field.toCharArray();  
        StringBuilder sb = new StringBuilder(); 
        sb.append(StringUtils.upperCase(CharUtils.toString(chars[0]))); 
        for (int i = 1; i < chars.length; i++) {  
            char c = chars[i];  
            if (c == '_') {  
                int j = i + 1;  
                if (j < chars.length) {  
                    sb.append(StringUtils.upperCase(CharUtils.toString(chars[j])));  
                    i++;  
                }  
            } else {  
                sb.append(c);  
            }  
        } 
        return sb.toString();  
    }
    
    /**
     * 将字符串的首字母大写
     * @param value 要转换的字符串
     * @return 将字符串首字母大写后的字符串
     */
    public static String upperFirstChar(String value){
    	if(null==value){
    		return "";
    	}
    	
    	return StringUtils.capitalize(value);
    }
    
    /**
     * 将字符串的首字母小写
     * @param value 要转换的字符串
     * @return 将字符串首字母小写后的字符串
     */
    public static String lowerFirstChar(String value){
    	if(null==value){
    		return "";
    	}
    	
    	return StringUtils.uncapitalize(value);
    }
}
